from sqlalchemy import create_engine, String, Integer, Float
from sqlalchemy.orm import declarative_base, sessionmaker, Session, mapped_column, Mapped

# 声明基类Base
Base = declarative_base()
# 创建引擎，连接到数据库
engine = create_engine('mysql+pymysql://root:root@localhost:3306/zijie', echo=True)

DBSession = sessionmaker(bind=engine)
db_session: Session = DBSession()


class TestCase(Base):
    __tablename__ = 'test_case'
    # 用例编号(Mapped:python ,需要指定注解)
    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    # 用例名称
    name: Mapped[str] = mapped_column(String(30))
    # URL地址
    url: Mapped[str] = mapped_column(String(100))
    # 请求方法
    method: Mapped[str] = mapped_column(String(10))
    # 请求参数
    params: Mapped[str] = mapped_column(String(100))
    # 请求体数据
    data: Mapped[str] = mapped_column(String(300))

    def as_dict(self):
        info = {k: v for k, v in self.__dict__.items() if not k.startswith('_')}
        return info




class Task(Base):
    __tablename__ = 'task'
    # 任务编号
    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    # 任务名称
    name: Mapped[str] = mapped_column(String(30))
    # 成功数
    success: Mapped[int] = mapped_column(Integer)
    # 总数
    total: Mapped[int] = mapped_column(Integer)
    # 成功率
    rate: Mapped[float] = mapped_column(Float)
    def as_dict(self):
        info = {k:v for k,v in self.__dict__.items() if not k.startswith('_')}
        return info

def creat():
    # 创建表
    Base.metadata.create_all(engine)
def drop():
    # 删除表
    Base.metadata.drop_all(engine)


if __name__ == '__main__':

    creat()

    # # 增加数据
    # add_case = TestCase(id=1, name='测试用例1', url='http://ceshiren.com', method='get', params='{"name": "ceshi"}',
    #                     data='{"name": "data"}')
    # db_session.add(add_case)
    # # 提交
    # db_session.commit()
    # # 关闭
    # db_session.close()
